<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Examination_model extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function getMyExaminationList($departmentid,$accountID,$per_page,$limit)
    {
        $sql = "SELECT a.*,b.paperID,b.paperTitle,b.examinationCustomer FROM tr_examination_list a,tr_examination_paper b WHERE a.examinationID=b.examinationID and a.examinationDept =b.examinationDept and a.examinationDept=".$departmentid."  ORDER BY a.createTime DESC ";
        $myExamiationTmpList = $this->_geQuerySql($sql);
        $myExamiationList = array('count'=>0,'list'=>array());
        //过滤出属于我的考试
        if(!empty($myExamiationTmpList)){
            $myExamiationTmpList = $this->_getMyExaminResutl($myExamiationTmpList,$accountID);
            if(!empty($myExamiationTmpList)){
                $myExamiationList['count'] = count($myExamiationTmpList);
                $myExamiationList['list'] = $this->spliceArrByPage($per_page,$limit,$myExamiationTmpList);
            }
        }

        return $myExamiationList;
    }
    /*
     * 获取属于我的考试 edit by cc at 2017/01/07
     */
    private function _getMyExaminResutl($myExamiationList,$accountID){
        if(!is_array($myExamiationList)) return array();
        $tmpArray = array();
        foreach($myExamiationList as $key=>$value){
            $accountList = json_decode($value['examinationCustomer'],true);
            if(in_array($accountID,$accountList)){
                $tmpArray[] = $value;
            }
        }
        return $tmpArray;

    }
    public function getPaperAnswerInfo($paperID){
        $sql = "select paper.examinationID,paper.paperID,question.questionID,question.questionType,answer.answer from tr_examination_paper as paper,tr_examination_question as question,tr_question_answer as answer where paper.paperID = question.paperID and question.questionID=answer.questionID and paper.paperID=".$paperID;
        $result = $this->_geQuerySql($sql);
        return $result;
    }

    public function getMyScoreList($accountID,$departmentid,$per_page,$limit){
        $limit = " limit {$per_page},{$limit}";
        $sql = "select myscore.*,mylist.examinationTitle,mylist.examinationTime from tr_user_score as myscore,tr_examination_list as mylist where myscore.examinationID=mylist.examinationID and myscore.departmentID=".$departmentid." and myscore.userID=".$accountID." order by myscore.createTime desc {$limit}";
        $result = $this->_geQuerySql($sql);
        return $result;
    }
    public function getMyScoreListCnt($accountID,$departmentid,$examinationID=null){
        $where = '';
        if(!empty($examinationID)){
            $where = " and examinationID=".$examinationID;
        }
        $sql = "select count(1) from tr_user_score where userID=".$accountID." and departmentID=".$departmentid.$where;
        $query = $this->db->query($sql);
        return $query->num_rows();
    }



    
}

/* End of file Document_model.php */
/* Location: ./application/models/document_model.php */